package me.lensfrex.doubandps;

import com.google.gson.*;
import me.lensfrex.doubandps.data.MovieInformation;
import me.lensfrex.doubandps.data.MovieInformationDataExporter;
import me.lensfrex.doubandps.utils.IOUtil;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.logging.Logger;

public class Main {
    private final static Logger log = Logger.getLogger("MainLogger");

    public static void main(String[] args) {
        new Main().run();
    }

    // 忙，先这样写吧
    // todo: 等什么时候稍微有空再改改
    private void run() {
        // information不可数，但我就是要数
        ArrayList<MovieInformation> movieInformations = new ArrayList<>();

        try {
            InputStream sourceFileStream = getClass().getResourceAsStream("/douban.json");
            if (sourceFileStream == null) {
                log.severe("源数据文件不存在，润了，不干了");
                return;
            }

            String sourceFileString = IOUtil.inputStreamToString(sourceFileStream, StandardCharsets.UTF_8);
            JsonArray dataArray = new Gson().fromJson(sourceFileString, JsonArray.class);

            for (int i = 0; i < dataArray.size(); i++) {
                movieInformations.add(new Gson().fromJson(dataArray.get(i), MovieInformation.class));
            }

            MovieInformationDataExporter movieInformationDataExporter = new MovieInformationDataExporter("./douban.xlsx", MovieInformation.class);

            movieInformationDataExporter.exportInformationByYear(movieInformations);

        } catch (IOException e) {
            log.severe("读文件出问题啦：" + e.getMessage());
        } catch (JsonSyntaxException e) {
            System.err.println("Json文件数据不对捏：" + e.getMessage());
        }
    }
}
